package com.ruoyi.yl.controller;

import com.ruoyi.yl.common.Result;
import com.ruoyi.yl.domain.YlStock;
import com.ruoyi.yl.domain.YlStockDrugInfo;
import com.ruoyi.yl.service.StockManageService;
import org.springframework.web.bind.annotation.*;
import static com.ruoyi.yl.constant.YlConstant.*;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;

/**
 * @Author: linKe
 * @Date: 2022/2/17 17:24
 * @FileName: StockArrangeManageController
 * @version: 1.0
 * @Description: 入库管理
 */
@RestController
@RequestMapping("/stockManage")
public class StockManageController {
    @Resource
    private StockManageService stockManageServiceImpl;

    /**
     * 查看所有入库信息
     */
    @GetMapping("/getAll")
    public Result<List<YlStock>> getAll() {
        List<YlStock> all = stockManageServiceImpl.getAll();
        return new Result<>(OP_SUCCESS_CODE, OP_SUCCESS_MSG, all);
    }

    /**
     * 根据id查入库信息
     */
    @GetMapping("/getOneById/{id}")
    public Result<List<YlStockDrugInfo>> getOneById(@PathVariable("id") Long id) {
        YlStockDrugInfo stockDrug = stockManageServiceImpl.getOneById(id);
        List<YlStockDrugInfo> ylStockDrugInfos = Collections.singletonList(stockDrug);
        return new Result<>(OP_SUCCESS_CODE, OP_SUCCESS_MSG, ylStockDrugInfos);
    }

    /**
     * 根据入库类型查询入库信息
     */
    @GetMapping("/getStocksByType/{id}")
    public Result<List<YlStock>> getStocksByType(@PathVariable("id") Long id) {
        List<YlStock> list = stockManageServiceImpl.getStocksByType(id);
        return new Result<>(OP_SUCCESS_CODE, OP_SUCCESS_MSG, list);
    }

    /**
     * 根据审核状态查询入库信息
     */
    @GetMapping("/getStocksByCheckStatus/{id}")
    public Result<List<YlStock>> getStocksByCheckStatus(@PathVariable("id") Long id) {
        List<YlStock> list = stockManageServiceImpl.getStocksByCheckStatus(id);
        return new Result<>(OP_SUCCESS_CODE, OP_SUCCESS_MSG, list);
    }

    /**
     * 根据输入框模糊查询入库信息
     */
    @GetMapping("/getStocksByLike")
    public Result<List<YlStock>> getStocksByLike(String dsc) {
        List<YlStock> stocksByLike = stockManageServiceImpl.getStocksByLike(dsc);
        return new Result<>(OP_SUCCESS_CODE, OP_SUCCESS_MSG, stocksByLike);
    }


    /**
     * 增加一条入库信息
     */
    @PostMapping("/addStock")
    public int addStock(YlStock stock) {
        long currentTimeMillis = System.currentTimeMillis();
        String second = "rk" + currentTimeMillis;

        StringBuilder str = new StringBuilder();
        //定义变长字符串
        Random random = new Random();
        //随机生成bai数字du，并添加zhi到字符串
        for (int i = 0; i < 8; i++) {
            str.append(random.nextInt(10));
        }
        //将字dao符串转换为数字并输出
        Long batchNumber = Long.parseLong(str.toString());

        //入库单号和批号不用前端传，后端生成
        YlStock stock1 = (YlStock)new YlStock().setNumber(second)
                .setStockDate(LocalDate.now())
                .setStockWorker(1L)
                .setStockType(1)
                .setTableMaker(1L)
                .setSupplier(1L)
                .setCheckStatus(1)
                .setCheckWorker(1L)
                .setBatchNumber(str.toString())
                .setPurchaseSum(new BigDecimal(223))
                .setSaleSum(new BigDecimal(24.2))
                .setCheckDate(LocalDate.of(2022, 2, 21))
                .setRemark("测试数据");

        return stockManageServiceImpl.addStock(stock1);
    }

}
